<template>
	<view>
		<view class="cu-custom " :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar fixed" :style="style" :class="[bgImage!=''?'none-bg text-white bg-img':'',bgColor]">
				<view class="action" @tap="BackPage" v-if="isBack">
					<text class="cuIcon-back"></text>
					<slot name="backText"></slot>
				</view>
				<view class="content content-box"  :style="[{top:StatusBar + 'px'}]">
					
					<slot name="content"></slot>
				</view>
				
				<slot name="right"></slot>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar
			};
		},
		name: 'cu-custom',
		computed: {
			style() {
				var StatusBar = this.StatusBar;
				var CustomBar = this.CustomBar;
				var bgImage = this.bgImage;
				var style = `height:${CustomBar-StatusBar}px;padding-top:${StatusBar}px;`;
				if (this.bgImage) {
					style = `${style}background-image:url(${bgImage});`;
				}
				return style
			}
		},
		props: {
			bgColor: {
				type: String,
				default: ''
			},
			isBack: {
				type: [Boolean, String],
				default: false
			},
			bgImage: {
				type: String,
				default: ''
			},
		},
		mounted() {

			this.getInfo().then((res) => {
				this.StatusBar = res.StatusBar;
				this.CustomBar = res.CustomBar;
			
			})
		},
		methods: {

			getInfo() {
				return new Promise((resolve, reject) => {
					uni.getSystemInfo({
						success: (e) => {
							let StatusBar = 0;
							let CustomBar = 44;
							// #ifndef MP
							StatusBar = e.statusBarHeight;
							if (e.platform == 'android') {
								CustomBar = e.statusBarHeight + 50;
							} else {
								CustomBar = e.statusBarHeight + 44;
							};
							// #endif

							// #ifdef MP-WEIXIN
							StatusBar = e.statusBarHeight;
							let custom = wx.getMenuButtonBoundingClientRect();

							CustomBar = custom.bottom + custom.top - e.statusBarHeight;
							// #endif		

							// #ifdef MP-ALIPAY
							StatusBar = e.statusBarHeight;
							CustomBar = e.statusBarHeight + e.titleBarHeight;
							// #endif

							let dat = {
								StatusBar: StatusBar,
								CustomBar: CustomBar
							}
							resolve(dat);
						}
					});
				})

			},


			BackPage() {
				uni.navigateBack({
					delta: 1
				});
			},
			rightTo() {
				this.$emit("jumpRight");
			}
		}
	}
</script>

<style scoped>
	
	@import url("/static/server/css/cu-bar.css");
	@import url("/static/server/css/icon.css");
	.right-text {
		position: absolute;
		float: right;
		right: 0;
		padding-right: 10rpx;

	}
	.cuIcon-back{
		width: calc(100vw*(22/390));
		height: calc(100vw*(22/390));
		display: flex;
		align-items: center;justify-content: center;
	}

	.bg-blue {
		background-color: #3951FB;
		color: #ffffff;
	}
	.content-box{
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
